<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import { register } from '@/api/user'
import type { RegisterParams } from '@/api/user'

const router = useRouter()
const loading = ref(false)

// 注册表单数据
const registerForm = reactive<RegisterParams>({
  username: '',
  password: '',
  nickname: '',
  phone: '',
  email: '',
  gender: 1,
  status: 1,
  address: ''
})

// 确认密码
const confirmPassword = ref('')

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名' },
    { minLength: 3, message: '用户名长度不能小于3个字符' }
  ],
  password: [
    { required: true, message: '请输入密码' },
    { minLength: 6, message: '密码长度不能小于6个字符' }
  ],
  confirmPassword: [
    { 
      required: true, 
      message: '请确认密码' 
    },
    {
      validator: (value: string) => value === registerForm.password,
      message: '两次输入的密码不一致'
    }
  ],
  nickname: [
    { required: true, message: '请输入昵称' }
  ],
  phone: [
    { required: true, message: '请输入手机号' },
    { 
      match: /^1[3-9]\d{9}$/, 
      message: '请输入正确的手机号码' 
    }
  ],
  email: [
    { required: true, message: '请输入邮箱' },
    { 
      match: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, 
      message: '请输入正确的邮箱格式' 
    }
  ],
  address: [
    { required: true, message: '请输入地址' }
  ]
}

// 处理注册
const handleRegister = async () => {
  // 验证确认密码
  if (registerForm.password !== confirmPassword.value) {
    Message.error('两次输入的密码不一致')
    return
  }
  
  loading.value = true
  try {
    await register(registerForm)
    
    Message.success('注册成功，请登录')
    
    // 跳转到登录页
    router.push('/login')
  } catch (error) {
    console.error('注册失败:', error)
    Message.error('注册失败，请稍后再试')
  } finally {
    loading.value = false
  }
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/login')
}

// 返回首页
const goToHome = () => {
  router.push('/')
}
</script>

<template>
  <div class="register-container">
    <div class="register-header">
      <a-button shape="circle" @click="goToHome">
        <template #icon><icon-left /></template>
      </a-button>
      <h2>用户注册</h2>
      <div></div>
    </div>

    <div class="register-form">
      <a-form
        :model="registerForm"
        :rules="rules"
        layout="vertical"
        @submit="handleRegister"
      >
        <a-form-item field="username" label="用户名">
          <a-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            allow-clear
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item field="password" label="密码">
          <a-input-password
            v-model="registerForm.password"
            placeholder="请输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>
        
        <a-form-item field="confirmPassword" label="确认密码">
          <a-input-password
            v-model="confirmPassword"
            placeholder="请再次输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>
        
        <a-form-item field="nickname" label="昵称">
          <a-input
            v-model="registerForm.nickname"
            placeholder="请输入昵称"
            allow-clear
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item field="phone" label="手机号">
          <a-input
            v-model="registerForm.phone"
            placeholder="请输入手机号"
            allow-clear
          >
            <template #prefix>
              <icon-phone />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item field="email" label="邮箱">
          <a-input
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            allow-clear
          >
            <template #prefix>
              <icon-email />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item field="gender" label="性别">
          <a-radio-group v-model="registerForm.gender">
            <a-radio :value="1">男</a-radio>
            <a-radio :value="2">女</a-radio>
            <a-radio :value="0">保密</a-radio>
          </a-radio-group>
        </a-form-item>
        
        <a-form-item field="address" label="地址">
          <a-input
            v-model="registerForm.address"
            placeholder="请输入地址"
            allow-clear
          >
            <template #prefix>
              <icon-location />
            </template>
          </a-input>
        </a-form-item>
        
        <div class="form-actions">
          <a-button type="text" @click="goToLogin">已有账号，去登录</a-button>
        </div>
        
        <a-button
          type="primary"
          long
          size="large"
          :loading="loading"
          html-type="submit"
        >
          注册
        </a-button>
      </a-form>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  padding: 16px;
  max-width: 500px;
  margin: 0 auto;
}

.register-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.register-header h2 {
  margin: 0;
}

.register-form {
  background-color: var(--color-bg-2);
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.form-actions {
  display: flex;
  justify-content: space-between;
  margin-bottom: 24px;
}
</style> 